package com.ch7.common;

import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Utility {

	public static String Substring(String input,int len){
		if(DataValidator.isNulloEmpty(input))
			return "";
		if(len>=input.length())
			return input;
		return input.substring(0,len);
	}
	
	public static Date getNowDateTime(){
		Calendar cal =Calendar.getInstance();
		String now =cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1)+"-"+
		cal.get(Calendar.DATE)+" "+cal.get(Calendar.HOUR)+":"+
				cal.get(Calendar.MINUTE)+":"+
		cal.get(Calendar.SECOND);
		return DataConverter.toDate(now, "yyyy-MM-dd HH:mm:ss");
	}
	
	public static Date getNowDate(){
		Calendar cal =Calendar.getInstance();
		String now =cal.get(Calendar.YEAR)+"-"+(cal.get(Calendar.MONTH)+1)+"-"+
		cal.get(Calendar.DATE);
		return DataConverter.toDate(now,"yyyy-MM-dd");
	}
	
	public static void writeCookie(HttpServletResponse response,String key,String value){
		writeCookie(response, key, value,-1);
	}

	public static void writeCookie(HttpServletResponse response, String key, String value, int i) {
		// TODO Auto-generated method stub
		Cookie newCookie=new Cookie(key,value);
		if(i>0)
			i=i*60;
		newCookie.setPath("/");
		newCookie.setMaxAge(i);
		response.addCookie(newCookie);
		
	}
	
	public static String readCookie(HttpServletRequest request,String key){
		String value ="";
		Cookie[] ck=request.getCookies();
		if(ck==null)
			return "";
		for(Cookie c:ck){
			if(c.getName().equals(key)){
				value=c.getValue();
				break;
			}
		}
		return value;
	}

}
